package com.wh.minio.container.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 时间工具类
 * @Package com.wh.technology.xbox.utils.date
 * @author 谢泽鹏
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
public class DateTool {

    private static final Log logger = LogFactory.getLog(DateTool.class);

    private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };
	
    
	private final static ThreadLocal<SimpleDateFormat> dateFormaterFull = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    private static SimpleDateFormat sysDateByAll = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    private static SimpleDateFormat sysDateByAllFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒SSS毫秒");
	private static SimpleDateFormat sysDateByAllFormat1 = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    private static SimpleDateFormat sysDateByFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat sysDateByFullFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");

    private static SimpleDateFormat sysDateByMinute = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private static SimpleDateFormat sysDateByMinuteFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

    private static SimpleDateFormat sysDateByHour = new SimpleDateFormat("yyyy-MM-dd HH");
    private static SimpleDateFormat sysDateByHourFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时");
	private static SimpleDateFormat sysDateByHourFormat1 = new SimpleDateFormat("yyyy年MM月dd日HH时");


    private static SimpleDateFormat sysDate = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sysDateFormat = new SimpleDateFormat("yyyy年MM月dd日");



    //private static SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat dateFormat3 = new SimpleDateFormat("yyyyMMdd");

    
	/**
	 * 获得当前系统时间并转换成字符串(格式：yyyy-MM-dd HH:mm:ss:SSS)
	 * @return String 当前系统时间
	 */
    public static String getSysDateByAll(){
    	return sysDateByAll.format(new Date());
    }
    
    
	/**
	 * 系统时间转换成指定格式(格式：yyyy年MM月dd日 HH时mm分ss秒SSS毫秒)
	 * @return String 当前系统时间
	 */
    public static String getSysDateByAllFormat(){
    	return sysDateByAllFormat.format(new Date());
    }

	public static String sysDateByAllFormat1(){
		return sysDateByAllFormat1.format(new Date());
	}


    
	/**
	 * 获得当前系统时间并转换成字符串(格式：yyyy-MM-dd HH:mm:ss)
	 * @return String 当前系统时间
	 */
	public static String getSysDateByFull(){
		return sysDateByFull.format(new Date());
	}
	
	
	/**
	 * 系统时间转换成指定格式(格式：yyyy年MM月dd日 HH时mm分ss秒)
	 * @return String 当前系统时间
	 */
	public static String getSysDateByFullFormat(){
		return sysDateByFullFormat.format(new Date());
	}
	
	
	/**
	 * 获得当前系统时间并转换成字符串(格式：yyyy-MM-dd HH:mm)
	 * @return String 当前系统时间
	 */
	public static String getSysDateByMinute(){
		return sysDateByMinute.format(new Date());
	}
	
	
	/**
	 * 系统时间转换成指定格式(格式：yyyy年MM月dd日 HH时mm分)
	 * @return String 当前系统时间
	 */
	public static String getSysDateByMinuteFormat(){
		return sysDateByMinuteFormat.format(new Date());
	}
	
	/**
	 * 获得当前系统时间并转换成字符串(格式：yyyy-MM-dd HH)
	 * @return String 当前系统时间
	 */
	public static String getSysDateByHour(){
		return sysDateByHour.format(new Date());
	}

	
	/**
	 * 系统时间转换成指定格式(格式：yyy年MM月dd日 HH时)
	 * @return String  当前系统时间
	 */
	public static String getSysDateByHourFormat(){
		return sysDateByHourFormat.format(new Date());
	}


	/**
	 * 系统时间转换成指定格式(格式：yyy年MM月dd日HH时)
	 * @return String  当前系统时间
	 */
	public static String getSysDateByHourFormat1(){
		return sysDateByHourFormat1.format(new Date());
	}
	
	/**
	 * 获得当前系统时间并转换成字符串(格式：yyyy-MM-dd)
	 * @return String 当前系统时间
	 */
	public static String getSysDate() {
		return sysDate.format(new Date());
	}

	/**
	 * 获得当前系统时间前一天并转换成字符串(格式：yyyy-MM-dd)
	 * @return
	 */
	public static String getSysDateBeforeDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		Date date = calendar.getTime();
		return sysDate.format(date);
	}

	/**
	 * 系统时间转换成指定格式(格式：yyy年MM月dd日)
	 * @return String  当前系统时间
	 */
	public static String getSysDateFormat(){
		return sysDateFormat.format(new Date());
	}
	
	
	/**
	 * 通过时间字符串转换成指定格式
	 * @date 2014年10月10日 下午4:09:44
	 * @param date 时间值
	 * @param dataFormat 格式为(yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss)
	 * @return String 返回格式化时间值
	 */
	public static Date getFormatDateByString(String date, String dataFormat) {
		try {
			return new SimpleDateFormat(dataFormat).parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			logger.error("getFormatDateByString->转换异常" + e.getMessage());
			return null;
		}
	}
	
	

	

	
	
	/**
	 * 通过时间字符串转换成指定格式(格式：yyyy年MM月dd日)
	 * @param date 时间值
	 * @return String 返回格式化时间值
	 */
	public static String getFormatDateByString(String date){
		try {
			return new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
			logger.error("getFormatDateByString->转换异常" + e.getMessage());
			return null;
		}
	}
	
	
	/**
	 * 通过时间字符串转换成指定格式(格式：yyyy年MM月dd日 HH时mm分ss秒)
	 * @param date 时间值
	 * @return String 返回格式化时间值
	 */
	public static String getFormatDateFullByString(String date){
		try {
			return new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
			logger.error("getFormatDateFullByString->>转换异常" + e.getMessage());
			return null;
		}
	}
	
	
	/**
	 * 获取系统时间(格式：yyyyMMddHHmmss)
	 * @return String 返回时间
	 */
	public static String getNowTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(Calendar.getInstance().getTime());
	}

	
	/**
	 * 获取系统时间(格式：yyyyMMddHHmmss)
	 * @return String 返回时间
	 */
	public static String getStringTime() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		return df.format(new Date());
	}
	
	
	/**
	 * 获取系统时间(格式：yyyyMMddHHmmssSSS)
	 * @return String 返回时间
	 */
	public static String getStringTimeFull() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return df.format(new Date());
	}
	
	


	/**
	 * 获取参数时间的年
	 * @param date 时间值
	 * @return String 返回年
	 */
    public static String getSysYear(Date date){ 
       return new SimpleDateFormat("yyyy").format(date).toString(); 
    } 
    
    
    /**
	 * 获取参数时间的年
	 * @param date 时间值
	 * @return String 返回年
	 */
    public static String getSysYear(String date){ 
        return new SimpleDateFormat("yyyy").format(date).toString(); 
     } 

    
    /**
     * 获取参数时间的月
     * @param date 时间值
     * @return String 返回月
     */
    public static String getSysMonth(Date date){ 
        return new SimpleDateFormat("MM").format(date).toString(); 
    } 
    
    /**
     * 获取参数时间的月
     * @param date 时间值
     * @return String 返回月
     */
    public static String getSysMonth(String date){ 
        return new SimpleDateFormat("MM").format(date).toString(); 
    } 
    
    
    /**
     * 获取参数时间的天
     * @param date  时间值
     * @return String 返回天
     */
    public static String getSysDay(Date date){ 
        return new SimpleDateFormat("dd").format(date).toString(); 
    } 
	
    
    /**
     * 获取参数时间的天
     * @param date 时间值
     * @return String 返回天 
     */
    public static String getSysDay(String date){ 
        return new SimpleDateFormat("dd").format(date).toString(); 
    } 
	
	
	/**
	 * 获取当前时间(格式：yyyy-MM-dd)
	 * @return String 返回时间
	 */
	public static String getCalendarToday() {
		int year = 0;
		int moth = 0;
		int day = 0;
		Calendar c = Calendar.getInstance();
		year = c.get(Calendar.YEAR);
		moth = c.get(Calendar.MONTH) + 1;
		day = c.get(Calendar.DAY_OF_MONTH);
		return year + "-" + moth + "-" + day;
	}
	

	/**
	 * 获取当前时间的下个月份(格式：yyyy-MM-dd)
	 * @return String 当前时间下个月份
	 */
	public static String getCalendarTodayNextMonth() {
		int year = 0;
		int moth = 0;
		int day = 0;
		Calendar c = Calendar.getInstance();
		year = c.get(Calendar.YEAR);
		moth = c.get(Calendar.MONTH) + 1;
		day = c.get(Calendar.DAY_OF_MONTH) + 2;
		return year + "-" + moth + "-" + day;
	}
	
	
	/**
	 * 判断日期是否属于今天日期(精确到天)
	 * @param sDate 日期值
	 * @return boolean 返回true表示是，false表示不是
	 */
    public static boolean getSysIsToday(String sDate) {
        boolean falg = false;
		try {
			Date date = null;
			date = dateFormaterFull.get().parse(sDate);
			Date today = new Date();
	        if (date != null) {
	            String nowDate = dateFormater.get().format(today);
	            String timeDate = dateFormater.get().format(date);
	            if (nowDate.equals(timeDate)) {
	            	falg = true;
	            }
	        }
		} catch (ParseException e) {
			e.printStackTrace();
			logger.error("getSysIsToday->>转换异常" + e.getMessage());
		}
        	return falg;
    }

    /**
     * 检查日期是否有效
     * @param year 年
     * @param month 月
     * @param day 日
     * @return boolean   
     */
	public static boolean getDateIsTrue(String year, String month, String day){
		try {
			String data = year + month + day;
			SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
			simpledateformat.setLenient(false);
			simpledateformat.parse(data);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("getDateIsTrue->>转换异常" + e.getMessage());
			return false;
		}
			return true;
	}
	
	
	/**
	 * 判断两个字符串日期的前后
	 * @param strdate1  字符串时间1
	 * @param strdate2  字符串时间2
	 * @return boolean   
	 */
	public static boolean getDateIsBefore(String strdate1, String strdate2){
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			logger.info("getDateIsBefore->>strdate1: " + strdate1);
			logger.info("getDateIsBefore->>strdate2: " + strdate2);
			return df.parse(strdate1).before(df.parse(strdate2));
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("getDateIsBefore->>Exception " + e.getMessage());
			return false;
		}
	}

	/**
	 * 判断两个字符串日期的前后
	 * @param Longdate1  字符串时间1
	 * @param Longdate2  字符串时间2
	 * @return boolean   
	 */
	public static boolean getDateIsBefore(Long Longdate1, Long Longdate2){
		try {
			logger.info("getDateIsBefore->>strdate1: " +  Longdate1 + "");
			logger.info("getDateIsBefore->>strdate2: " +  Longdate2 + "");
			Longdate1 = (null == Longdate1) ? 0 : Longdate1;
			Longdate2 = (null == Longdate2) ? 0 : Longdate2;
			return  Longdate1 > Longdate2 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("getDateIsBefore->>Exception " + e.getMessage());
			return false;
		}
	}
	
	
	
	/**
	 * 判断两个时间日期的前后
	 * @param date1  日期1
	 * @param date2  日期2
	 * @return boolean   
	 */
	public static boolean getDateIsBefore(Date date1, Date date2) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return getDateIsBefore(df.format(date1), df.format(date2));
	}


    /**
     * 获取今日开始时间(Sat Nov 05 00:00:00 CST 2016)
     * @param date
     * @return
     */
    public static Date getDateStart(Date date) {
        Calendar c = new GregorianCalendar();
        c.clear();
        Calendar co = new GregorianCalendar();
        co.setTime(date);
        c.set(Calendar.DAY_OF_MONTH,co.get(Calendar.DAY_OF_MONTH));
        c.set(Calendar.MONTH,co.get(Calendar.MONTH));
        c.set(Calendar.YEAR,co.get(Calendar.YEAR));
        //c.add(Calendar.DAY_OF_MONTH,1);
        //c.add(Calendar.MILLISECOND,-1);
        return c.getTime();
    }

    /**
     * 获取今日结束时间(Sat Nov 05 23:59:59 CST 2016)
     * @param date
     * @return
     */
    public static Date getDateEnd(Date date) {
        Calendar c = Calendar.getInstance();
        c.clear();
        Calendar co = Calendar.getInstance();
        co.setTime(date);
        c.set(Calendar.DAY_OF_MONTH,co.get(Calendar.DAY_OF_MONTH));
        c.set(Calendar.MONTH,co.get(Calendar.MONTH));
        c.set(Calendar.YEAR,co.get(Calendar.YEAR));
        c.add(Calendar.DAY_OF_MONTH,1);
        c.add(Calendar.MILLISECOND,-1);
        return c.getTime();
    }


    /**
     * 格式为2016-11-05 00:00:00.0
     * @return
     */
    public static Timestamp getCurrentDate(){
        Calendar c = Calendar.getInstance();
        c.set(c.get(1), c.get(2), c.get(5), 0, 0, 0);
        Timestamp t = new Timestamp(c.getTime().getTime());
        t.setNanos(0);
        return t;
    }


    /**
     * 格式为2016-11-05
     * @param data
     * @return
     */
    public static Date dateToSQLDate(Date data){
        return data != null ? new Date(data.getTime()) : null;
    }


    /**
     * 格式为23:31:47
     * @param data
     * @return
     */
    public static Time dateToSQLTime(Date data){
        return data != null ? new Time(data.getTime()) : null;
    }

    /**
     * 格式为2016-11-05 23:31:47.905
     * @param data
     * @return
     */
    public static Timestamp dateToSQLTimestamp(Date data){
        return data != null ? new Timestamp(data.getTime()) : null;
    }

    /**
     * 格式为Sat Nov 05 23:38:48 CST 2016
     * @param timestamp
     * @return
     */
    public static Date sqlTimestampToDate(Timestamp timestamp){
        return timestamp != null ? new Date(Math.round((double)timestamp.getTime() + (double) timestamp.getNanos() / 1000000D)) : null;
    }


    /**
     * 格式为
     * getDate()：Mon Dec 05 23:59:59 CST 2016
     * getDate()：Mon Dec 05 00:00:00 CST 2016
     * @param y
     * @param m
     * @param d
     * @param inclusive
     * @return
     */
    public static Date getDate(int y, int m, int d, boolean inclusive){
        Date dt = null;
        Calendar c = Calendar.getInstance();
        c.clear();
        if(c.getActualMinimum(1) <= y && y <= c.getActualMaximum(1))
        {
            c.set(1, y);
            if(c.getActualMinimum(2) <= m && m <= c.getActualMaximum(2))
            {
                c.set(2, m);
                if(c.getActualMinimum(5) <= d && d <= c.getActualMaximum(5))
                    c.set(5, d);
            }
            if(inclusive)
            {
                c.add(5, 1);
                c.add(14, -1);
            }
            dt = c.getTime();
        }
        return dt;
    }


    /**
     * 时间转成日期时间
     * @param time
     * @return
     */
    public static long timeToDay(long time){
        long t = 0;
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = new Date(time);
        String day = sf.format(d);
        try {
            t = sf.parse(day).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * **
     * 获取年月日字符串   2013-06-02
     * @param date
     * @return
     */
    public static String getDateStr(Date date) {
        //如果时间为空，则获取当前时间
        if (null == date) {
            return sysDate.format(new Date());
        }
        return sysDate.format(date);
    }

    public static String getDateStr() {
        return getDateStr(null);
    }

    /**
     * *
     * 获取带是时分秒的日期字符串   2013-06-02 10:10:10
     * @param date
     * @return
     */
    public static String getLongDateStr(Date date) {
        if (null == date) {
            return sysDateByFull.format(new Date());
        }
        return sysDateByFull.format(date);
    }

    public static String getLongDateStr1(Date date) {
        if (null == date) {
            return sysDate.format(new Date());
        }
        return sysDate.format(date);
    }


    public static String getLongDateStr() {
        return getLongDateStr(null);
    }

    /**
     * 获取8位数字的字符串  20130602
     *
     * @param date
     * @return
     */
    public static String getDateNum(Date date) {
        if (null == date) {
            return dateFormat3.format(new Date());
        }
        return dateFormat3.format(date);
    }

    public static String getDateNum() {
        return getDateNum(null);
    }


    public static Date parse(String dateString, String style)
            throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(style);
        return dateFormat.parse(dateString);
    }

    public static String format(Date date, String style) {
        if(null == date){
            return "";
        }
        DateFormat dateFormat = new SimpleDateFormat(style);
        return dateFormat.format(date);
    }


    /***
     * BigDecimal 类型时间数据转 string
     * @param bigDecimal
     * @return
     */
    public static String getDateLongToStr(BigDecimal bigDecimal){
        if(null == bigDecimal && bigDecimal.longValue() == 0){
            return "";
        }

        Date date = new Date(bigDecimal.longValue());
        return sysDateByFull.format(date);
    }


	/***
	 * BigDecimal 类型时间数据转 不带时分秒  string
	 * @param bigDecimal
	 * @return
	 */
	public static String getDateLongToSysDateFormatStr(BigDecimal bigDecimal){
		if(null == bigDecimal && bigDecimal.longValue() == 0){
			return "";
		}

		Date date = new Date(bigDecimal.longValue());
		return sysDate.format(date);
	}

	/***
	 * BigDecimal 类型时间数据转 YYYY-MM-DD HH:SS  string
	 * @param bigDecimal
	 * @return
	 */
	public static String getDateLongToDateByMinuteStr(BigDecimal bigDecimal){
		if(null == bigDecimal && bigDecimal.longValue() == 0){
			return "";
		}

		Date date = new Date(bigDecimal.longValue());
		return sysDateByMinute.format(date);
	}

    /***
     * 获取倒计时
     * @param wait
     * @return
     */
    public static String getCountDown(double wait){
        int	day=0,hour=0,minute=0,second=0;//默认值
        day=(int)Math.floor(wait/(60 * 60*24));
        hour =(int) Math.floor(wait/ (60 * 60))-(day*24);
        minute =(int)Math.floor(wait/ 60) - (hour * 60)-(day*60*24);
        second =(int)Math.floor(wait)-(hour * 60 * 60) - (minute * 60)-(day*60*60*24);
        if(day>0){
            return day+"天";
        }
        if(hour>0){
            return hour+"小时";
        }
        if(minute>0){
            return minute+"分钟";
        }
        if(second>0){
            return second+"秒";
        }
        return "0";
    }


    /*
     * 获取发标倒计时
     * 判断当期日期是否大于发标日期
     * verifyTime  发标日期
     */
//    public static Long countDown(Long verifyTime){
//        Long nowTime = System.currentTimeMillis();
//        if(nowTime >= verifyTime){
//            return 0L;
//        }else{
//            Long countDown = verifyTime - nowTime;
//            countDown = countDown / 1000;
//            return countDown;
//        }
//    }


    /**
     * 倒计时时间 + 1秒(由于网络请求耗时原因导致倒计时不精确问题)
     * @param nowTime 系统时间
     * @param verifyTime 初审时间
     * @return
     */
    public static Long countDown(Long nowTime, Long verifyTime) {
        if (verifyTime == null) {
            return 0L;
        }
        if (nowTime >= verifyTime) {
            return 0L;
        } else {
            Long countDown = verifyTime - nowTime;
            countDown = countDown / 1000;
            return countDown + 1;
        }
    }


    /**
	 * 得到某天加上天数的时间戳
	 * @param date     日期
	 * @param dayNum   天数
	 * @return
	 */
	public static final Long getThisDateDayLongTime(Date date ,Integer dayNum){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, dayNum);
		Date date1 = calendar.getTime();
		return date1.getTime();
	}



    /**
     * 获取 date 时间 23:59:59 000
     *
     * @param date
     * @return
     */
    public static Date getLastSecIntegralTime(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date.getTime());
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.SECOND, 51);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取当前时间 23:59:59 000
     *
     * @return
     */
    public static Date getLastSecIntegralTime() {

        return getLastSecIntegralTime(new Date());
    }


    public static void main(String[] args){


		System.out.println("sysDateByAllFormat1()：" +sysDateByAllFormat1());
        System.out.println("getCurrentDate()：" +getCurrentDate());
        System.out.println("getDateStart()：" + getDateStart(new Date()));
        System.out.println("getDateEnd()：" + getDateEnd(new Date()));
        System.out.println("dateToSQLDate()：" + dateToSQLDate(new Date()));
        System.out.println("dateToSQLTime()：" + dateToSQLTime(new Date()));
        System.out.println("dateToSQLTimestamp()：" + dateToSQLTimestamp(new Date()));
        System.out.println("sqlTimestampToDate()：" + sqlTimestampToDate(dateToSQLTimestamp(new Date())));
        System.out.println("getDate()：" + getDate(2016,11,5, true));
        System.out.println("getDate()：" + getDate(2016,11,5, false));

    }


	public static int[] getNeturalAge(Calendar calendarBirth,Calendar calendarNow) {
		int diffYears = 0, diffMonths, diffDays;
		int dayOfBirth = calendarBirth.get(Calendar.DAY_OF_MONTH);
		int dayOfNow = calendarNow.get(Calendar.DAY_OF_MONTH);
		if (dayOfBirth <= dayOfNow) {
			diffMonths = getMonthsOfAge(calendarBirth, calendarNow);
			diffDays = dayOfNow - dayOfBirth;
			//if (diffMonths == 0)
				diffDays++;
		} else {
			if (isEndOfMonth(calendarBirth)) {
				if (isEndOfMonth(calendarNow)) {
					diffMonths = getMonthsOfAge(calendarBirth, calendarNow);
					diffDays = 0;
				} else {
					calendarNow.add(Calendar.MONTH, -1);
					diffMonths = getMonthsOfAge(calendarBirth, calendarNow);
					diffDays = dayOfNow + 1;
				}
			} else {
				if (isEndOfMonth(calendarNow)) {
					diffMonths = getMonthsOfAge(calendarBirth, calendarNow);
					diffDays = 0;
				} else {
					calendarNow.add(Calendar.MONTH, -1);// 上个月
					diffMonths = getMonthsOfAge(calendarBirth, calendarNow);
					// 获取上个月最大的一天
					int maxDayOfLastMonth = calendarBirth.getActualMaximum(Calendar.DAY_OF_MONTH);
					if (maxDayOfLastMonth > dayOfBirth) {
						diffDays = maxDayOfLastMonth - dayOfBirth + dayOfNow;
					} else {
						diffDays = dayOfNow;
					}
				}
			}
		}
		// 计算月份时，没有考虑年
		diffYears = diffMonths / 12;
		diffMonths = diffMonths % 12;
		return new int[] { diffYears, diffMonths, diffDays };
	}


	/**
	 * 获取两个日历的月份之差
	 *
	 * @param calendarBirth
	 * @param calendarNow
	 * @return
	 */
	public static int getMonthsOfAge(Calendar calendarBirth,
									 Calendar calendarNow) {
		return (calendarNow.get(Calendar.YEAR) - calendarBirth
				.get(Calendar.YEAR))* 12+ calendarNow.get(Calendar.MONTH)
				- calendarBirth.get(Calendar.MONTH);
	}


	/**
	 * 判断这一天是否是月底
	 *
	 * @param calendar
	 * @return
	 */
	public static boolean isEndOfMonth(Calendar calendar) {
		int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
		if (dayOfMonth == calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
			return true;
		return false;
	}


}
